<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use think\Request;

class Replymessage extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function reply_gz()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('category',$catregory);
        return $this->fetch('replymessage/reply_gz');
    }
    public function reply_pt()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('category',$catregory);
        return $this->fetch('replymessage/reply_pt');
    }
    public function getMessageList()
    {
        $keyword = $this->request->post('keyword','');
        $category = $this->request->post('category','');
        $category2= $this->request->post('category2','');
        $type= $this->request->post('type','');
        if($category !== ''){
            $map[] = ['category', '=', $category];
        }
        if($category2 !== ''){
            $map[] = ['category2','=', $category2];
        }
        $map[] = ['delstatus', '=', 1];
        $map[] = ['type', '=', $type];
        $list = Db::table('nx_imgtext_info')
            ->where('id','like','%'.$keyword.'%')
            ->where($map)
            ->order('category','desc')
            ->select();
        foreach($list as $k=>$v){
            $list[$k]['category'] = '';
            $list[$k]['category2'] = '';
            if($v['category']!=''){
                $categorylist = Db::table('nx_category')->where('id',$v['category'])->find();
                $list[$k]['category'] = $categorylist['categoryname'];
                if($categorylist){
                    $catregory2 = Db::table('nx_category')->where('id',$v['category2'])->find();
                    $list[$k]['category2'] = $catregory2['categoryname'];
                }
            }
        }
        return  $this->api_resp_success("",'',$list);
    }
    public function reply_detail()
    {
        $id = $this->request->param('id');
        $list = Db::table('nx_imgtext_info')->where(['id'=>$id])->field('title,desc,img_url,t_url')->find();
        $this->assign('list',$list);
        return $this->fetch('replymessage/reply_detail');
    }
    public function editreply()
    {
        $id = $this->request->param('id');
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $catregory2 = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>2
            ])
            ->select();
        $this->assign('category',$catregory);
        $this->assign('category2',$catregory2);
        $list = Db::table('nx_imgtext_info')->where(['id'=>$id])->find();
        $this->assign('list',$list);
        return $this->fetch('replymessage/editreply');
    }
    public function doEditReply()
    {
        $id = $this->request->post('id');
        $title = $this->request->post('title');
        $category = $this->request->post('category');
        $category2 = $this->request->post('category2');
        $desc = $this->request->post('desc');
        $t_url = $this->request->post('t_url');
        $img_url = $this->request->post('img_url');
        $data = [
            'category'=>$category,
            'title'=>$title,
            'category2'=>$category2,
            'desc'=>$desc,
            'img_url'=>$img_url,
            't_url'=>$t_url,
            'updatetime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_imgtext_info')->where('id',$id)->update($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }else{
            return $this->api_resp_error('');
        }
    }
    public function reply_key()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('category',$catregory);
        return $this->fetch('replymessage/reply_key');
    }
    public function addKeyReply()
    {
        $catregory = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $catregory2 = Db::table('nx_category')
            ->where([
                'delstatus'=>1,
                'level'=>2
            ])
            ->select();
        $this->assign('category',$catregory);
        $this->assign('category2',$catregory2);
        return $this->fetch('replymessage/addKeyReply');
    }
}
